<script setup lang="ts">
import { onBeforeRouteUpdate, useRoute, useRouter } from 'vue-router'
import { ref, reactive, computed, onMounted, toRefs } from 'vue';
import { showNotify, showToast } from 'vant';
import { default as http, getImageUrl } from '@/utils/request.js';
const router = useRouter(); //获取到 router 对象，用于编程式跳转导航
const route = useRoute();   //获取路由对象
function toAddReturn() {
    router.push("/addreorder");
}

//地址列表
const onClickLeft = () => history.back();
// 折叠面板
const activeNames = ref(['1']);

//h
//route.params.xxx 获取路径参数
console.log('orderInfo.orderNumber = ', route.params.orderNumber);
const props = defineProps(['orderNumber', 'title']);
// onBeforeRouteUpdate((to, from ) => {
//     console.log("onBeforeRouteUpdate!!!!",to.params.orderNumber)
//     if(to.params.orderNumber !== from.params.orderNumber){
//         // console.log("这里可以请求服务器获取id = ",props.ordernumber)
//     }
// })

const orderInfo = reactive({
    address: '',
    orderState: '',
    receivingTime: '',//签收时间
    sellerName: '',
    amount: '',
    orderNumber: '',
    orderTime: '',
    details: '',
    goodsName: '',
    orderPrice: '',
    payTime: '',
    phoneNum: '',
    picture: '',
    trackingNumber: '',//物流单号
    price: ''
})
// 获取订单详情
const getOrderInfo = () => {
    console.log("------------订单详情");
    http.get('/user/order/ordernumber/' + route.params.orderNumber).then((res) => {
        orderInfo.address = res.data.data.address;
        orderInfo.receivingTime = res.data.data.receivingTime;
        orderInfo.sellerName = res.data.data.sellerName;
        orderInfo.amount = res.data.data.amount;
        orderInfo.orderNumber = res.data.data.orderNumber;
        orderInfo.details = res.data.data.details;
        orderInfo.goodsName = res.data.data.goodsName;
        orderInfo.orderPrice = res.data.data.orderPrice;
        orderInfo.payTime = res.data.data.payTime;
        orderInfo.phoneNum = res.data.data.phoneNum;
        orderInfo.picture = res.data.data.picture;
        orderInfo.trackingNumber = res.data.data.trackingNumber;
        orderInfo.orderState = res.data.data.orderState;
        orderInfo.orderTime = res.data.data.orderTime;
        orderInfo.price = res.data.data.price;
        console.log("物流单号:", orderInfo.trackingNumber);

        // resolve(); // 当获取待收货列表成功后，返回一个解析成功的Promise  
    }).catch(err => {
        //   reject(err); // 当请求失败时，返回一个请求失败的Promise  
    });

}
// 弹出框
const show = ref(false);
// const onChange = (show) => (show.value = true);

const reorder = reactive({
    // reorderId:'',
    sellerName: '',
    amount: '',
    orderNumber: '',
    createTime: '',
    details: '',
    goodsName: '',
    orderPrice: '',
    reason: '',
    moneyBackTime: '',
    sellerOpinion: '',
    returnWay: '',
    returnStatus: '',
    picture: '',
    trackingNumber: '',//物流单号
})

const onFailed = () => {
    showNotify({ type: 'success', message: '通知内容' });
}
const addReturn = () => {
    reorder.orderNumber=orderInfo.orderNumber;
    // 二次确认
    http.post("/user/returnorder",reorder,{headers: {'Content-Type': 'application/json'}})
        .then((res) => {
            console.log('!!!!!!!!!!!!!!!!!返回值：', res.data);
            const { code, message } = res.data;
            console.log("=========code",code);
            if (200 === code) {
                showNotify({ type: 'success', message: '申请成功' });
                setTimeout(() => {
                    router.push("/returnorder"); //使用路径导航
                }, 1000);
            } else {
                showNotify({ type: 'danger', message });
            }
        }).catch((err) => {
            console.log('申请失败：', err);
        });
}
const handleConfirm=()=>{
    addReturn();
} 
onMounted(() => {
    getOrderInfo();
    // handleDelete(); 
    // 获取个人信息  

});
</script >


<template>
    <van-col class="nav_bar">
        <van-nav-bar title="订单详情" left-text="返回" :fixed="true" left-arrow @click-left="onClickLeft" />
    </van-col>
    <!-- </header> -->
    <div style="margin-top: 30px;">

    </div>
    <div style="width: 350px; margin-top: 10px">

        <van-cell-group inset style="width: 350px;">
            <van-cell>
                <van-row style="margin-left: 8px;">
                    <van-col>
                        <div>订单状态：{{ orderInfo.orderState }}</div>
                    </van-col>
                </van-row>
            </van-cell>
            <van-cell>
                <van-row style="margin-left: 8px;">
                    <van-col>
                        <div>签收时间：{{ orderInfo.receivingTime }}</div>
                    </van-col>
                </van-row>
            </van-cell>
            <van-cell>
                <van-row style="margin-left: 8px;">
                    <van-col>
                        <div>物流单号：{{ orderInfo.trackingNumber }}</div>
                    </van-col>
                </van-row>
            </van-cell>
            <van-cell>
                <van-row style="margin-left: 8px;">
                    <van-row>
                        <van-col>
                            <div>订单地址：{{ orderInfo.address }}</div>
                        </van-col>
                    </van-row>
                </van-row>
            </van-cell>
            <van-cell>
                <div style="width:300px; color: #c8d175; margin-top: 10px;">
                    <van-card :num=orderInfo.amount :price=orderInfo.price :desc=orderInfo.details :title=orderInfo.details
                        :thumb="getImageUrl(orderInfo.picture)" size="large">
                        <template #tags>
                            <van-tag plain type="primary" style="flex-grow: 1; margin-right: 10px;">{{ orderInfo.goodsName
                            }}</van-tag>
                        </template>

                    </van-card>
                </div>
            </van-cell>
            <van-cell>
                <van-row style="margin-left: 8px;">
                    <van-col>
                        <div>商品总价：{{ orderInfo.orderPrice }}</div>
                    </van-col>
                </van-row>
            </van-cell>
            <van-cell>
                <van-row style="margin-left: 8px;">
                    <van-col>
                        <div>实付款：{{ orderInfo.orderPrice }}</div>
                    </van-col>
                </van-row>
            </van-cell>
            <van-cell>
                <van-row style="margin-left: 8px;">
                    <van-row>
                        <div>订单编号：{{ orderInfo.orderNumber }}</div>
                    </van-row>
                </van-row>
            </van-cell>
            <van-cell>
                <van-row style="margin-left: 8px;">
                    <van-row>
                        <div>下单时间：{{ orderInfo.orderTime }}</div>
                    </van-row>
                </van-row>
            </van-cell>
            <van-cell>
                <van-row style="margin-left: 8px;">
                    <van-row>
                        <div>付款时间：{{ orderInfo.payTime }}</div>
                    </van-row>
                </van-row>
            </van-cell>

        </van-cell-group>
        <van-cell>
            <van-row style="margin-left: 10px;">
                <van-row>
                    <van-button name="button" @click="show = true">申请售后</van-button>
                </van-row>

                <van-dialog v-model:show="show" title="申请售后"   width="300" height="300">
                    <van-cell-group>
                        <van-cell>
                            <van-row style="margin-left: 8px;">
                                <van-row span="4">
                                    <van-col>
                                        <van-radio-group v-model="reorder.returnWay" shape="dot" direction="horizontal">
                                            <van-radio name="1">仅退款</van-radio>
                                            <van-radio name="2">退货退款</van-radio>
                                        </van-radio-group>
                                    </van-col>
                                </van-row>
                            </van-row>
                        </van-cell>
                        <van-cell>
                            <!-- v-model="" -->
                            <van-field v-model="reorder.reason" reason="原因" label="退款原因" placeholder="请输入退款原因" required
                                :rules="[{ required: true, message: '请输入退款原因' }]" />
                        </van-cell>
                        
                    </van-cell-group>
                    <template #footer>
					<span class="dialog-footer">
						<van-button @click="show = false">取 消</van-button>
						<van-button type="primary" @click="handleConfirm">确 定</van-button>
					</span>
				</template>
                </van-dialog>
            </van-row>
        </van-cell>
    </div>
    <br>

    <RouterView />
</template>

<style scoped>
.nav_bar {
    position: relative;
    top: -32px;
    /* left:-32px;
    right: -32px; */
    margin: 0px -32px
}

header {
    position: fixed;
    top: 0;
    width: 100%;
    z-index: 9999;
    line-height: 1.5;
    max-height: 100vh;
}


header {
    line-height: 1.5;
    max-height: 100vh;
}


nav {
    width: 100%;
    font-size: 12px;
    text-align: center;
    margin-top: 2rem;
}

nav a.router-link-exact-active {
    color: var(--color-text);
}

nav a.router-link-exact-active:hover {
    background-color: transparent;
}

nav a {
    display: inline-block;
    padding: 0 1rem;
    border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
    border: 0;
}

@media (min-width: 1024px) {
    header {
        display: flex;
        place-items: center;
        padding-right: calc(var(--section-gap) / 2);
    }


    header .wrapper {
        display: flex;
        place-items: flex-start;
        flex-wrap: wrap;
    }

    nav {
        text-align: left;
        margin-left: -1rem;
        font-size: 1rem;
        padding: 1rem 0;
        margin-top: 1rem;
    }
}

.van-field {
    /* 调整每一行边距 */
    padding: 16px 15px;
}

.van-nav-bar {
    color: #d4e09f;
    width: 100%;
}

.full-width-card {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 10px;
    /* 根据需要调整 */
}

.template {
    background-color: #F0F8FF;
}

.container {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    /* 如果需要垂直居中 */
}

.full-width-button {
    flex: 1;
    /* 使按钮填充整个容器的宽度 */
}

.body {
    background-color: #e2e3df;
}

.img-icon {
    height: 20px;
}

.nav_bar {
    position: relative;
    top: -32px;
    /* left:-32px;
    right: -32px; */
    margin: 2px -32px
}
</style>
